package org.beans.expense.common.page;

import org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxFallbackDefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.beans.expense.common.CommonConstants;
import org.beans.expense.common.domain.DailyExpense;
import org.beans.expense.common.panel.DataTablePanel;
import org.beans.expense.common.service.IDailyExpenseService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author: arunachalam
 * @since: 2/16/13
 */
public class DailyExpensesPage extends AbstractBasePage
{
    private DailyExpenseDataProvider dataProvider = new DailyExpenseDataProvider();
    private IDailyExpenseService dailyExpenseService = (IDailyExpenseService) new ClassPathXmlApplicationContext(
            CommonConstants.CONTEXT_CONFIG_FILE).getBean("dailyExpenseService");
    //@SpringBean
    //private IDailyExpenseService dailyExpenseService;

    public DailyExpensesPage()
    {
        super();
        add(createPageHeader());
        add(createAddExpenseLink());
        add(createDataTableContainer());
    }

    private Link createAddExpenseLink()
    {
        return new Link("addExpenseLink")
        {
            @Override
            public void onClick()
            {
                setResponsePage(new AddEditDailyExpensePage(new DailyExpense()));
            }
        };
    }

    private WebMarkupContainer createDataTableContainer()
    {
        WebMarkupContainer dataTableContainer = new WebMarkupContainer("dataTableContainer");
        dataTableContainer.add(createDataTable());
        dataTableContainer.setOutputMarkupId(true);
        return dataTableContainer;
    }

    private DataTable createDataTable()
    {
        List<IColumn> columns = new ArrayList<IColumn>();

        columns.add(new AbstractColumn<DailyExpense, String>(new Model<String>(getString("description")))
        {
            @Override
            public void populateItem(Item<ICellPopulator<DailyExpense>> cellItem, String componentId, final IModel<DailyExpense> rowModel)
            {
                cellItem.add(new DataTablePanel(componentId, rowModel.getObject().getDescription())
                {
                    @Override
                    protected void onEdit()
                    {
                        setResponsePage(new AddEditDailyExpensePage(rowModel.getObject()));
                    }

                    @Override
                    protected void onDelete()
                    {
                        dailyExpenseService.delete(rowModel.getObject());
                    }
                });
            }
        });
        columns.add(new PropertyColumn(new Model(getString("amount")), "amount"));
        columns.add(new PropertyColumn(new Model(getString("dateTime")), "dateTime"));
        return new AjaxFallbackDefaultDataTable("dataTable", columns, dataProvider, CommonConstants.ITEMS_PER_PAGE);
    }

    private class DailyExpenseDataProvider extends SortableDataProvider<DailyExpense, String>
    {

        @Override
        public Iterator iterator(long first, long last)
        {
            List<DailyExpense> dailyExpenses = dailyExpenseService.findAll();
            return dailyExpenses.iterator();
        }

        @Override
        public SortParam getSort()
        {
            return super.getSort() != null ? super.getSort() : new SortParam("firstName", true);
        }

        @Override
        public long size()
        {
            return dailyExpenseService.countAll();
        }

        @Override
        public IModel<DailyExpense> model(DailyExpense object)
        {
            return Model.of(object);
        }

        @Override
        public void detach()
        {
        }
    }
}
